/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.Color;
import java.awt.Font;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import net.worlds.input.InputState;
import net.worlds.lua.Event;
import net.worlds.ui.edge.EActionListener;
import net.worlds.ui.edge.EButton;
import org.jbox2d.common.Vec2D;


public class UITextButton extends UIComponent{
    public UITextButton() throws WorldsException
    {
        super("UITextButton");
        component = new EButton("Button");
        ((EButton)component).setActionListener(new EActionListener()
        {
            @Override
            public void actionPerformed() {
                if(Main.isClient())
                {
                  Main.getClient().sendButtonClick(UITextButton.this, InputState.mouseButton, InputState.mouseX, InputState.mouseY);
                }
                getEvent("MouseDown").fire(new Object[]{InputState.mouseButton, new Vec2D(InputState.mouseX, InputState.mouseY)});
            }
        });
        addPropChangeHandler("Text", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                ((EButton)component).setText(value.toString());
            }
        });
        addPropChangeHandler("FontName", new PropChangeHandler() {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(inWorld)
                    ((EButton)component).setFont(new Font(value.toString(), -1, getIntProp("FontSize")));
            }
        });
        addPropChangeHandler("FontSize", new PropChangeHandler() {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                int i = (Integer) value;
                if(inWorld)
                    ((EButton)component).setFont(new Font(getStringProp("FontName"), -1, i));
            }
        });
        addPropChangeHandler("BackgroundColor", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                ((EButton)component).setColor(((Color)value));
            }
        });
        addPropChangeHandler("TextColor", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                ((EButton)component).setTextColor(((Color)value));
            }
        });
        addEvent("MouseDown", new Event(), "button (integer), screenPosition (Vec2D)");
    }
    @Override
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
        ((EButton)component).setFont(new Font(getStringProp("FontName"), -1, getIntProp("FontSize")));
    }
}
